﻿using System.Diagnostics;
using System.Threading;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;

using ZQNB.BaseLib.Accounts.Impl;
using ZQNB.Common;
using ZQNB.Common.Encrypt;
using ZQNB.Common.NHExtensions;
using ZQNB.Entity.Accounts;

namespace ZQNB.BaseLib.Accounts
{
    //对于带有 AssemblyInitialize 属性标记的方法，仅当该方法属于带有 TestClass 属性标记的类的成员时，测试引擎才会运行它
    [TestClass]
    public class SchemaTest
    {
        private static bool _inited = false;
        [AssemblyInitialize]
        public static void SetupTestEnvForEachClass(TestContext context)
        {
            Debug.WriteLine("Assembly Init => ");
            if (_inited)
            {
                return;
            }
            //RbacNinjectLocator.Instance.SetupAutoMapper();
            //生成数据库结构
            SqlUtHelper.Instance = new MyUtHelper();
            //SqlUtHelper.Instance.ReCreateDbIfExist(".", "NbV3DbTest", "sa", "zqnb_123");
            SqlUtHelper.Instance.CreateDbIfNotExist(".", "NbV3DbTest", "sa", "zqnb_123");
            SqlUtHelper.Instance.SetUpNHibernate(true, typeof(AccountMap));

            //准备测试数据
            SqlUtHelper.Instance.InitData();
            _inited = true;
        }

        [AssemblyCleanup]
        public static void AssemblyCleanup()
        {
            Debug.WriteLine("Assembly Cleanup => ");
        }

        [TestMethod]
        public void CreateSchemaTest()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
        }

        [TestMethod]
        public void CreateShould()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
            LocalAccountService service = new LocalAccountService(session, new AccountPolicy(), new PasswordEncryptor(new NbEncryptHelper()));
            Account admin = TestInitData.Clone(TestInitData.WhhcAdminAccount);

            MessageResult messageResult = service.Create(admin);
            Assert.IsTrue(messageResult.Success);
        }

        [TestMethod]
        public void CreateExistShould()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
            LocalAccountService service = new LocalAccountService(session, new AccountPolicy(), new PasswordEncryptor(new NbEncryptHelper()));
            Account exist = TestInitData.Clone(TestInitData.ExistAccount);

            MessageResult messageResult = service.Create(exist);
            Assert.IsFalse(messageResult.Success);
        }
        
        [TestMethod]
        public void ValidateExistShould()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
            LocalAccountService service = new LocalAccountService(session, new AccountPolicy(), new PasswordEncryptor(new NbEncryptHelper()));
            Account exist = TestInitData.Clone(TestInitData.ExistAccount);

            var result = service.Validate(exist.LoginName, exist.Password);
            Assert.IsTrue(result);
            var result2 = service.Validate(exist.LoginName, "xxx");
            Assert.IsFalse(result2);
        }
        
        [TestMethod]
        public void GetAccountShould()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
            LocalAccountService service = new LocalAccountService(session, new AccountPolicy(), new PasswordEncryptor(new NbEncryptHelper()));
            Account exist = TestInitData.Clone(TestInitData.ExistAccount);

            var result = service.GetAccount(exist.LoginName);
            Assert.IsNotNull(result);
            
            var result2 = service.GetAccount(exist.Email);
            Assert.IsNotNull(result2);

            var result3 = service.GetAccount(exist.CustomNo);
            Assert.IsNotNull(result3);

            var result4 = service.GetAccount("xxx");
            Assert.IsNull(result4);
        }
        
        [TestMethod]
        public void ResetPasswordShould()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
            var accountPolicy = new AccountPolicy();
            LocalAccountService service = new LocalAccountService(session, accountPolicy, new PasswordEncryptor(new NbEncryptHelper()));

            Account admin = TestInitData.Clone(TestInitData.WhhcAdminAccount); 
            service.Create(admin);

            var result = service.ResetPassword(admin.LoginName);
            Assert.IsTrue(result);
        }
        
        [TestMethod]
        public void DeleteShould()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
            var accountPolicy = new AccountPolicy();
            LocalAccountService service = new LocalAccountService(session, accountPolicy, new PasswordEncryptor(new NbEncryptHelper()));

            Account exist = TestInitData.Clone(TestInitData.WhhcAdminAccount); 
            service.Create(exist);

            var result = service.Delete(exist.LoginName);
            Assert.IsTrue(result.Success);
        }

        [TestMethod]
        public void UpdateShould()
        {
            ISession session = SqlUtHelper.Instance.OpenSession();
            var accountPolicy = new AccountPolicy();
            LocalAccountService service = new LocalAccountService(session, accountPolicy, new PasswordEncryptor(new NbEncryptHelper()));

            Account exist = service.GetAccount(TestInitData.ExistAccount.LoginName);
            
            exist.AccountPk = "whhc";

            var result = service.Update(exist);
            Assert.IsTrue(result.Success);

            
        }
    }
}